package com.shell.governor.modules.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.governor.modules.po.PubDictionary;
import com.shell.governor.modules.service.PubDictionaryService;

@RestController
public class PubDictionaryController extends BaseController {
	
	@Autowired
	private PubDictionaryService pubDictionaryService;

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public List<PubDictionary> selectDictionarys(PubDictionary dictionary) throws Exception {
		return this.pubDictionaryService.getAllDicTreeList(dictionary);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����ID��������")
	public PubDictionary findDictionary(String dicId) throws Exception {
		return this.pubDictionaryService.findDictionaryByDicId(dicId);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����/��������")
	public RetMsg save(PubDictionary dic) throws Exception {
		if (this.pubDictionaryService.saveOrUpdateDictionary(dic) != 1) {
			return initRetMsg("success", "��������������");
		}
		return initRetMsg("success", "��������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg updateDictionary(PubDictionary dictionary) throws Exception {
		if (this.pubDictionaryService.saveOrUpdateDictionary(dictionary) != 1) {
			return initRetMsg("error", "��������������");
		}
		return initRetMsg("success", "��������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public RetMsg updateDictionaryStateWithCascade(String dicId, String parentId, String inUse) throws Exception {
		System.out.println(dicId + "---" + inUse);
		String msg = inUse.equals("U") ? "����" : inUse.equals("D") ? "����" : "����";
		if (this.pubDictionaryService.updateResourceStateWithCascade(dicId, parentId, inUse) > 0) {
			return initRetMsg("success", msg + "����������");
		}
		return initRetMsg("error", msg + "����������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public PubDictionary select(String id) {
		return (PubDictionary) this.pubDictionaryService.selectByKey(id);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������������������")
	public RetMsg checkCode(PubDictionary dictionary) {
		return initRetMsg("success", false, Boolean.valueOf(this.pubDictionaryService.checkCodeUnique(dictionary)));
	}
}
